package singleton;

import java.io.Serializable;

/**
 * 饿汉式  只要类初始化该实例就已被创建，提前创建
 */
public class Singleton1 implements Serializable {
    private Singleton1(){
        //防止通过反射技术来破环单例
        if(INSTANCE != null){
            throw new RuntimeException("单例对象不能重复创建");
        }
        System.out.println("private Singleton1()");
    }

    private static final Singleton1 INSTANCE = new Singleton1();

    public static  Singleton1 getInstance(){
        return INSTANCE;
    }

    //防止反序列化破环单例
    public Object readResolve(){
        return INSTANCE;
    }
}
